package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.mapper.AddressBookMapper;
import com.itheima.reggie.pojo.AddressBook;
import com.itheima.reggie.service.AddressBookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook> implements AddressBookService {



    @Transactional
    @Override
    public void setDefaultAddr(Long id) {
        this.lambdaUpdate()
                .set(AddressBook::getIsDefault,0)
                .eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .update();
        this.lambdaUpdate()
                .set(AddressBook::getIsDefault,1)
                .eq(AddressBook::getId,id)
                .update();
    }

    @Override
    public AddressBook getDefaultAddr() {
        return this.lambdaQuery()
                .eq(AddressBook::getIsDefault, 1)
                .one();
    }
}
